/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bc;

import edu.upn.sigemfi.ope.bc.VentanillaFacadeLocal;
import edu.upn.sigemfi.ope.be.Ventanilla;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Gabo (U)
 */
public class VentanillaDelegate {

    private VentanillaFacadeLocal local;

    public VentanillaDelegate() {
        this.local = lookupVentanillaFacade();
    }

    public void crear(Ventanilla ventanilla) throws Exception {
        try {
            local.create(ventanilla);
        } catch (Exception ex) {
            local = lookupVentanillaFacade();
            throw new Exception("Crear Ventanilla... " + ex.getMessage());
        }
    }

    public void editar(Ventanilla ventanilla) throws Exception {
        try {
            local.edit(ventanilla);
        } catch (Exception ex) {
            local = lookupVentanillaFacade();
            throw new Exception("Editar Ventanilla... " + ex.getMessage());
        }
    }

    public void eliminar(Ventanilla ventanilla) throws Exception {
        try {
            local.remove(ventanilla);
        } catch (Exception ex) {
            local = lookupVentanillaFacade();
            throw new Exception("Eliminar Ventanilla... " + ex.getMessage());
        }
    }

    public List<Ventanilla> getVentanillas() throws Exception {
        List<Ventanilla> ventanillas;
        try {
            ventanillas = local.findAll();
        } catch (Exception ex) {
            local = lookupVentanillaFacade();
            throw new Exception("Listar Ventanillas... " + ex.getMessage());
        }
        return ventanillas;
    }

    public Ventanilla buscar(Long id) throws Exception {
        Ventanilla ventanilla;
        try {
            ventanilla = local.find(id);
        } catch (Exception ex) {
            local = lookupVentanillaFacade();
            throw new Exception("Buscar Ventanilla... " + ex.getMessage());
        }
        return ventanilla;
    }

    private VentanillaFacadeLocal lookupVentanillaFacade() {
        try {
            Context c = new InitialContext();
            return (VentanillaFacadeLocal) c.lookup("java:comp/env/VentanillaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
